package controllers

import (
	"github.com/gin-gonic/gin"
	"my-gin-tiktok/app/daos"
	"my-gin-tiktok/app/services"
	"my-gin-tiktok/common/response"
	"strconv"
)

// Upload 上传视频
func Upload(ctx *gin.Context) {
	var form daos.Video
	if err := ctx.ShouldBind(&form); err != nil {
		response.ValidateFail(ctx, err.Error())
		return
	}

	userID, _ := strconv.ParseInt(ctx.Keys["id"].(string), 10, 64)
	err := services.VideoService.CreateVideo(ctx, userID, form.Title, form.PlayURL, form.CoverURL)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, nil)
}

// Down 删除视频
func Down(ctx *gin.Context) {
	var form daos.Video
	if err := ctx.ShouldBind(&form); err != nil {
		response.ValidateFail(ctx, err.Error())
		return
	}

	userID, _ := strconv.ParseInt(ctx.Keys["id"].(string), 10, 64)
	err := services.VideoService.DeleteVideo(ctx, userID, form.ID)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}
	response.Success(ctx, nil)
}

// VideoList 视频列表
func VideoList(ctx *gin.Context) {
	videos, err := services.VideoService.ListVideo(ctx)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}

	response.Success(ctx, videos)
}

// VideoDetail 视频详情
func VideoDetail(ctx *gin.Context) {
	var form daos.Video
	if err := ctx.ShouldBind(&form); err != nil {
		response.ValidateFail(ctx, err.Error())
		return
	}

	video, err := services.VideoService.GetVideo(ctx, form.ID)
	if err != nil {
		response.BusinessFail(ctx, err.Error())
		return
	}

	response.Success(ctx, video)
}
